nlSortApp = angular.module('nlSortApp', ['ui.bootstrap']);

// Index page main controller
nlSortApp.controller('IndexController', function ($scope, $http, $modal, $log) {
    $http.get('entities').success(function (data) {
        $scope.entities = data;
    });

    $scope.add = function (entity) {
        $http.post(url = '/entity', data = entity)
            .success(function (data, status, headers, config) {
                console.log('success:' + data)
                $scope.entities.push(data)
            })
            .error(function (data, status, headers, config) {
                console.log('error:' + data)
            });
    };

    $scope.open = function (entity) {
        $log.info('open: ' + JSON.stringify(entity));
        var modalInstance = $modal.open({
            templateUrl: 'static/update.html',
            controller: 'UpdateInstanceController',
            resolve: {
                entity: function () {
                    return entity;
                }
            }
        });

        modalInstance.result.then(function (updatedEntity) {
            $scope.updatedEntity = updatedEntity;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

});

nlSortApp.controller('UpdateInstanceController', function ($scope, $modalInstance, $log, entity) {
    $scope.entity = entity;
    $log.info('Dialog entity: ' + entity);

    $scope.ok = function () {
        $modalInstance.close($scope.entity)
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel')
    };
});
